#include "cameraProvider.h"

char jpegBuffer[10000000];

Provider::Provider() : QQuickImageProvider(QQuickImageProvider::Image){

}


QImage Provider::requestImage(const QString &id, QSize *size, const QSize &requestedSize){

    return *pm;
}


CameraProvider::CameraProvider(QQmlApplicationEngine* engine)
{
    _image = new QImage();

    _provider = new Provider();
    _provider->pm = _image;
    engine->addImageProvider(QLatin1String("cameraimage"), _provider);
}

void CameraProvider::run(){
    while(1){
        captureImage();
        emit sigRefresh();
        Sleep(20);
    }
}


void CALLBACK g_ExceptionCallBack2(DWORD dwType, LONG lUserID, LONG IHandle, void *pUser){

    char tempbuf[256] = {0};
    switch(dwType){
    case EXCEPTION_RECONNECT:
        printf("--- reconnect ---%d\n", time(NULL));
        break;
    default:
        break;
    }

}
void CameraProvider::startCamera(){


    qDebug() << ">>> startCamera [Start]";

    NET_DVR_Init();
    NET_DVR_SetConnectTime(2000, 1);
    NET_DVR_SetReconnect(10000, true);
    NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack2, NULL);

    // 注册设备
    // 登录参数，包括设备地址、登录用户、密码等
    NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
    struLoginInfo.bUseAsynLogin = 0;
    strcpy(struLoginInfo.sDeviceAddress, "192.168.0.64");   // 设备IP地址
    struLoginInfo.wPort = 8000; // 设备服务端口
    strcpy(struLoginInfo.sUserName, "admin");
    strcpy(struLoginInfo.sPassword, "bokun@1243");
    // 设备信息、输出参数
    NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0};
    lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
    if(lUserID < 0){
        qDebug() << ">>> Login failed, error code: " << NET_DVR_GetLastError();
        NET_DVR_Cleanup();
        return;
    }
    qDebug() << ">>> Login Succeed";



    NET_DVR_PREVIEWINFO struPlayInfo = {0};
    struPlayInfo.hPlayWnd = NULL;   // 需要SDK解码时句柄设为有效值，仅取流不解码时可设为空
    struPlayInfo.lChannel = 1;      // 预览通道号
    struPlayInfo.dwStreamType = 0;  // 0-主码流 1-子码流 2-码流3 ...
    struPlayInfo.dwLinkMode = 0;    // 0-TCP 1-UDP 2-多播 3-RTP 4-RTP/RTSP 5-RSTP/HTTP
    struPlayInfo.bBlocked = 1;      // 0-非阻塞取流 1-阻塞取流

    lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, NULL, NULL);
    if(lRealPlayHandle < 0){
        qDebug() << ">>> NET_DVR_RealPlay_V40 error";
        NET_DVR_Logout(lUserID);
        NET_DVR_Cleanup();
        return;
    }
    qDebug() << ">>> startCamera [Finished]";
    isConnected = true;
}

void CameraProvider::startCapturing(){

//    if(_timer) delete _timer;
//    _timer = new QTimer();
//    _timer->setInterval(100);
//    connect(_timer, SIGNAL(timeout()), this, SLOT(captureImage()));
//    _timer->start();

}

void CameraProvider::captureImage(){

    NET_DVR_JPEGPARA jpegPara = {0};
    jpegPara.wPicSize = 500;
    jpegPara.wPicQuality = 2;
    DWORD resSize;
    NET_DVR_CaptureJPEGPicture_NEW(lUserID, 1, &jpegPara, jpegBuffer, (DWORD)10000000, &resSize);

    _image->loadFromData((uchar*)jpegBuffer, (uint)resSize);
    _provider->pm = _image;

//    qDebug() << "CaptureImage ThreadId" << QThread::currentThreadId();


}
